Starting from Scratch: Basic Web Application

In this part, we will walk you through getting a basic web application running in the cloud on AWS. We will start with a blank project, and build the application and its infrastructure step by step. Each step focuses on a single aspect of the infrastructure, and we will try to explain in detail what’s happening, and why.

All the source code shown in this guide is available on GitHub. Each commit represents a code checkpoint from the book, and you can find it all here: AWS Bootstrap.

When interacting with AWS, we will use both the AWS console and the AWS CLI. In addition, we will also make use of the following tools:

GitHub as the source code repository for our application and infrastructure code.

node.js and npm to build our application.

git for version control.

curl to interact with our application.

Objective#

  • Get a simple web application running on a single EC2 instance.

Steps#

  • Write a basic “hello world” web application.

In this section, we will create a tiny web application and we will get it running on an EC2 instance in an AWS account. We will start by performing all the steps manually, and we will automate them in later sections. Our application is not very interesting, and the way it will be hosted is far from ideal, but it will allow us to spend the rest of this book building a well-contained AWS setup, step by step.

Creating our application#

We will need git and npm installed. But don’t worry, these are already installed on our platform. So you can straight away use them in our terminal widget.

Now, let’s create our bare-bones application, along with a git repository to store it.

terminal

Our application is going to listen for HTTP requests on port 8080 and respond with “Hello World”. The entire application will be in one file, server.js.

server.js

⚠️ Line #4: we’ll run the server on port 8080 because port numbers below 1024 require root privileges.

We can run our application directly with the node command.

terminal

And we can test it with curl from another terminal window.

terminal

All these commands are already executed in the below widget. Hit on the RUN button to start the server.

/
server.js
Your app can be found at: https://811lgmnxmw1xy.educative.run

Next, let’s use a process manager to monitor our application so that it automatically restarts if it crashes. To do this, we need to modify our package.json file.

package.json

Line #11: Takes a dependency on pm2, a node process manager.

Line #7: From now on, we’ll use npm start to start our application via pm2.

Line #7: pm2 will monitor it under the name hello_aws and send its stdout to ../logs/app.log

Line #8: We’ll use npm stop to tell pm2 to stop our application.

Line #9: A dummy build step. Your actual production build process goes here.

Now, we need to use npm to get the new dependency we added in package.json.

terminal

In package.json we specified that the application’s logs are sent to ../logs. Having the directory for logs outside the application’s directory will be important when we deploy this with CodeDeploy, since it prevents the logs directory from being deleted with every deployment. For now, let’s create the directory manually on our local machine.

terminal

And now we should be able to start our application through the process manager.

terminal

Testing via curl should once again show our “Hello World” message.

terminal

Again, all these commands are already executed in the widget below. Hit on the RUN button to start the server.

/
server.js
package.json
Your app can be found at: https://811lgmnxmw1xy.educative.run

And with everything working, we can now commit all our changes to git.

terminal

Pushing our code to GitHub#

If you don’t already have a GitHub account, create one (it’s free for simple projects like this).

Next, create a new GitHub repository. We named ours aws-bootstrap. The repository needs to be public for now, but we’ll be able to make it private once we set up CodeBuild.

Now that we have a GitHub repository, let’s push our local changes to GitHub.

terminal

Line #1: replace <username> with your GitHub username.

Again all these git commands are executed. You just need to set your Github username as the environment variable first.

NOTE: When you run the code, it will ask you your username and password before pushing the files to Github. So don’t miss that!

This code requires the following environment variables to execute:
username
Not Specified...
Github_Token
Not Specified...
/
server.js
package.json
Your app can be found at: https://811lgmnxmw1xy.educative.run

In the next lesson, we will host our application and manually install it on an EC2 instance.

Quiz - Basic AWS Services
Starting from Scratch: Manual AWS Infrastructure
Mark as Completed
Report an Issue